/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.common.utils;

import cn.ac.nercis.pes.common.constant.common.BaseConstants;
import cn.hutool.core.date.LocalDateTimeUtil;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * 类型转换辅助类
 * @author zpy
 * @version 1.0.0
 */
public class ConvertUtils {
    private final static DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private final static DateTimeFormatter LOCAL_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private final static DateTimeFormatter FILE_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    private ConvertUtils() {
    }

    public static String toString(LocalDateTime dateTime) {
        if (Objects.isNull(dateTime)) {
            return "";
        }
        return DATETIME_FORMATTER.format(dateTime);
    }

    public static String toFileNameString(LocalDateTime dateTime){
        if (Objects.isNull(dateTime)) {
            return "";
        }
        return FILE_DATE_FORMATTER.format(dateTime);
    }

    public static String toString(LocalDate localDate) {
        if (Objects.isNull(localDate)) {
            return "";
        }
        return LOCAL_DATE_FORMATTER.format(localDate);
    }

    public static String toString(Integer value) {
        if (Objects.isNull(value)) {
            return "";
        }
        return String.valueOf(value);
    }

    public static LocalDate toLocalDate(String date) {
        if (StringUtils.isEmpty(date)) {
            return LocalDate.now();
        }
        return LocalDateTimeUtil.parseDate(date, LOCAL_DATE_FORMATTER);
    }

    public static int toInteger(String content){
        if(StringUtils.isEmpty(content)){
            return 0;
        }
        try{
            return Integer.parseInt(content);
        }catch (NumberFormatException ignore){
            return 0;
        }
    }

    public static double toDouble(String content){
        if(StringUtils.isEmpty(content)){
            return 0;
        }
        try{
            return Double.parseDouble(content);
        }catch (NumberFormatException ignore){
            return 0;
        }
    }

    public static String scoreToString(Double score){
        return scoreToString(score, BaseConstants.EMPTY_SCORE_TAG);
    }

    public static String scoreToString(Double score,String emptyScore){
        if(Objects.isNull(score)){
            return emptyScore;
        }
        if(score%1 == 0){
            return String.valueOf(score.intValue());
        }else{
            return String.valueOf(score);
        }
    }

    public static String scoreToString(Float score,String emptyScore){
        if(Objects.isNull(score)){
            return emptyScore;
        }
        if(score%1 == 0){
            return String.valueOf(score.intValue());
        }else{
            return String.valueOf(score);
        }
    }

    public static Double stringToScore(String score){
        if(StringUtils.isBlank(score)){
            return null;
        }
        return Double.parseDouble(score);
    }

}